package com.cmcc.linlaker.ui.map;

import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.ui.query.bean.QueryProjectBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.ToastUtils;
import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapController;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.MyLocationOverlay;
import com.tianditu.android.maps.TDrivingRoute;
import com.tianditu.android.maps.TDrivingRouteResult;
import com.tianditu.android.maps.overlayutil.TdrivingRouteOverlay;

import java.util.ArrayList;

public class NaviActivity extends AppCompatActivity implements TDrivingRoute.OnDrivingResultListener {
    public QueryProjectBean.QueryProjectData projectDetailData;
    public QueryProjectBean.QueryProjectData houseDetailData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navi);

        BarUtils.initStatusBar(this);

        projectDetailData = (QueryProjectBean.QueryProjectData) getIntent().getSerializableExtra("projectDetailData");
        houseDetailData = (QueryProjectBean.QueryProjectData) getIntent().getSerializableExtra("houseDetailData");

        initView();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    private LinearLayout layout_sl, layout_yx;
    private TextView text_sl, text_yx;
    private boolean isSl = true;
    public MapView mMapView;
    private ProgressDialog dialog;
    private ArrayList<GeoPoint> mAvePoints = new ArrayList<>();

    private void initView() {
        dialog = new ProgressDialog(this);
        dialog.setMessage("正在规划路线，请稍等...");

        mMapView = findViewById(R.id.navi_mapview);
        layout_sl = findViewById(R.id.layout_sl);
        layout_yx = findViewById(R.id.layout_yx);
        text_sl = findViewById(R.id.text_sl);
        text_yx = findViewById(R.id.text_yx);

        layout_sl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMapView.setMapType(MapView.TMapType.MAP_TYPE_VEC);

                layout_yx.setBackgroundColor(getResources().getColor(R.color.transparent));
                layout_sl.setBackgroundColor(getResources().getColor(R.color.white));

                text_sl.setVisibility(View.VISIBLE);
                text_yx.setVisibility(View.INVISIBLE);

                isSl = !isSl;
            }
        });

        layout_yx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMapView.setMapType(MapView.TMapType.MAP_TYPE_IMG);

                layout_yx.setBackgroundColor(getResources().getColor(R.color.white));
                layout_sl.setBackgroundColor(getResources().getColor(R.color.transparent));

                text_sl.setVisibility(View.INVISIBLE);
                text_yx.setVisibility(View.VISIBLE);

                isSl = !isSl;
            }
        });

        initMap();
    }

    public MyLocationOverlay mMyLocation;
    private GeoPoint mLocationPoint;
    private MapController mMapController;
    private TDrivingRoute mRoute = null;
    private TdrivingRouteOverlay mTrOverlay = null;

    final double LON = 119.041202; // 起始经度
    final double LAT = 29.611058; // 起始纬度

    private void initMap() {
        //得到mMapView的控制权,可以用它控制和驱动平移和缩放
        mMapController = mMapView.getController();
        //设置地图zoom级别
        mMapController.setZoom(12);
        //设置启用内置的缩放控件
        mMapView.setBuiltInZoomControls(true);
        mMapView.setMapType(MapView.TMapType.MAP_TYPE_VEC);
        mMapView.setLogoPos(MapView.LOGO_LEFT_TOP);

        mMyLocation = new MyOverlay(this, mMapView);
        mMapView.addOverlay(mMyLocation);
        getOverlay().enableMyLocation();

//        mLocationPoint = mMyLocation.getMyLocation();
        mLocationPoint = new GeoPoint((int) (LAT * 1E6), (int) (LON * 1E6));
        GeoPoint endPoint;
        if (projectDetailData != null) {
            endPoint = new GeoPoint((int) (Float.parseFloat(projectDetailData.latitude) * 1E6), (int) (Float.parseFloat(projectDetailData.longitude) * 1E6));
        } else {
            endPoint = new GeoPoint((int) (Float.parseFloat(houseDetailData.latitude) * 1E6), (int) (Float.parseFloat(houseDetailData.longitude) * 1E6));
        }
        mAvePoints.add(mLocationPoint);
        mAvePoints.add(endPoint);

        mRoute = new TDrivingRoute(this);
        mRoute.startRoute(mLocationPoint, endPoint, null,
                TDrivingRoute.DRIVING_TYPE_NOHIGHWAY);
        dialog.show();
    }


    private MyLocationOverlay getOverlay() {
        return mMyLocation;
    }

    @Override
    public void onDrivingResult(TDrivingRouteResult result, int errCode) {
        dialog.dismiss();

        if (errCode != 0) {
            ToastUtils.showText(this, "规划出错");
            return;
        }
        mTrOverlay = new TdrivingRouteOverlay(this, mMapView.getResources()
                .getDrawable(R.mipmap.ysh2_icon));
        mTrOverlay.setData(result);
        mMapView.addOverlay(mTrOverlay);
        mMapView.postInvalidate();
    }

    class MyOverlay extends MyLocationOverlay {
        public MyOverlay(Context context, MapView mapView) {
            super(context, mapView);
        }

        /*
         * 处理在"我的位置"上的点击事件
         */
        protected boolean dispatchTap() {
            return true;
        }

        @Override
        public void onLocationChanged(Location location) {
            super.onLocationChanged(location);

        }

    }

    public void back(View view) {
        finish();
    }

}
